import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import 'package:kq_flutter_widgets/theme/kq_theme_manager.dart';
import 'package:kq_flutter_core_widget/utils/ex/string_ex.dart';
import 'package:kq_flutter_core_widget/widgets/button/kq_ink_well.dart';
import 'package:kq_flutter_widgets/widgets/divider/kq_divider.dart';
import 'package:kq_flutter_widgets/widgets/image/kq_print_image.dart';
import 'package:kq_flutter_widgets/widgets/overflowBox/kq_over_flow_box.dart';
import 'package:kq_flutter_widgets/widgets/wrapFlow/kq_auto_column_flow.dart';

import '../../resources/kq_theme_colors.dart';

enum SimpleDividePosition {
  /// 头部
  top,

  /// 底部
  bottom
}

/// 只有标题，没有图片和副标题
class KqSimpleCard extends StatefulWidget {
  /// 标题
  final String? title;

  /// 分隔线位置,未设置就不显示
  final SimpleDividePosition? dividePosition;

  /// 分隔线高度 默认：1.r
  final double? divideHeight;

  /// 底部widget,和bottomBtns实现一种
  final Widget? bottomBtnsView;

  /// 底部widget,和bottomBtnsView实现一种
  final List<Widget>? bottomBtns;

  /// tags 标签
  final List<Widget>? tags;

  /// 印章类型，使用[KqPrintType]
  final KqPrintType? printType;

  /// 卡片信息内容个数
  final int totalCount;

  /// 行数
  final int Function()? rowCountCallback;

  ///每行的列数
  final int Function(int row)? renderCrossAxisCountCallback;

  ///icon
  final String Function(int row, int column)? renderIconPathCallback;

  ///Text
  final String Function(int row, int column)? renderTextCallback;

  /// 指定位置的右边widget自定义
  final Widget? Function(int row, int column)? renderWidgetCallback;

  ///Text行数
  final int Function(int row, int column) renderTextMaxLineCallback;

  /// 点击
  final void Function()? onTap;

  const KqSimpleCard(
      {this.title,
      this.dividePosition,
      this.divideHeight,
      this.bottomBtnsView,
      this.bottomBtns,
      this.tags,
      this.printType,
      this.onTap,
      required this.totalCount,
      required this.renderCrossAxisCountCallback,
      this.renderIconPathCallback,
      this.renderTextCallback,
      this.renderWidgetCallback,
      required this.rowCountCallback,
      required this.renderTextMaxLineCallback,
      super.key});

  @override
  State<KqSimpleCard> createState() => _KqSimpleCardState();
}

class _KqSimpleCardState extends State<KqSimpleCard> {
  double widgetWidth = KqScreenUtil().screenWidth;

  /// 水平间距
  double spacing = 8.r;

  /// 垂直方向间距
  double runSpacing = 6.r;

  @override
  Widget build(BuildContext context) {
    return Container(
      color: KqThemeColors.bgWhite,
      child: KqInkWell(
        onTap: () {
          widget.onTap?.call();
        },
        enableRipple: false,
        child: renderContent(),
      ),
    );
  }

  Widget renderContent() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: [
        Visibility(
            visible: widget.dividePosition != null &&
                widget.dividePosition == SimpleDividePosition.top,
            child: KqDivider(
              height: widget.divideHeight ?? 1.r,
            )),
        SizedBox(
          height: 10.r,
        ),
        renderTitleInfo(),
        SizedBox(
          height: 10.r,
        ),
        Container(
          margin: EdgeInsets.only(bottom: 12.r),
          padding: EdgeInsets.symmetric(horizontal: 16.r),
          child: renderWrapFlowContent(),
        ),
        Visibility(
            visible: widget.bottomBtns != null || widget.bottomBtnsView != null,
            child: Padding(
              padding: EdgeInsets.symmetric(horizontal: 16.r),
              child: const KqDivider(),
            )),
        Visibility(
            visible: widget.bottomBtns != null || widget.bottomBtnsView != null,
            child: renderBottomBtns()),
        Visibility(
            visible: widget.dividePosition != null &&
                widget.dividePosition == SimpleDividePosition.bottom,
            child: KqDivider(
              height: widget.divideHeight ?? 1.r,
            )),
      ],
    );
  }

  List<Widget> renderTags() {
    List<Widget> tags = [];
    if (widget.tags != null) {
      for (var i = 0; i < widget.tags!.length; i++) {
        tags.add(widget.tags![i]);
        if (i != widget.tags!.length - 1) {
          tags.add(SizedBox(
            width: 6.r,
          ));
        }
      }
    }
    return tags;
  }

  Widget renderTitleInfo() {
    String title = widget.title ?? '';
    return Container(
      margin: EdgeInsets.only(left: 12.r),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Expanded(
              child: Text(title.ellipsis(),
                  style: TextStyle(
                      fontSize:
                          KqThemeManager.getFormConfig().titleFontSize ?? 14.sp,
                      color: KqThemeColors.text26),
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis)),
          Visibility(
              visible: widget.tags != null,
              child: Container(
                margin: EdgeInsets.only(top: 3.r),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: renderTags(),
                ),
              )),
        ],
      ),
    );
  }

  Widget renderWrapFlowContent() {
    if (widget.printType != null) {
      return KqOverFlowBox(
          referenceWidget: KqAutoColumnFlow(
              spacing: spacing,
              runSpacing: runSpacing,
              fontSize: 12.r,
              iconWidth: 12.r,
              totalCount: widget.totalCount,
              renderCrossAxisCountCallback: widget.renderCrossAxisCountCallback,
              renderIconPathCallback: widget.renderIconPathCallback,
              renderTextCallback: widget.renderTextCallback,
              rowCountCallback: widget.rowCountCallback,
              renderTextMaxLineCallback: widget.renderTextMaxLineCallback),
          overflowBoxWidget: KqPrintImage(printType: widget.printType!),
          visible: true,
          position: BoxToReferencePosition.bottom,
          alignment: Alignment.bottomRight,
          maxWidth: 44.r,
          maxHeight: 44.r);
    }
    return KqAutoColumnFlow(
        spacing: spacing,
        runSpacing: runSpacing,
        fontSize: 12.r,
        iconWidth: 12.r,
        totalCount: widget.totalCount,
        renderCrossAxisCountCallback: widget.renderCrossAxisCountCallback,
        renderIconPathCallback: widget.renderIconPathCallback,
        renderTextCallback: widget.renderTextCallback,
        rowCountCallback: widget.rowCountCallback,
        renderWidgetCallback: widget.renderWidgetCallback,
        renderTextMaxLineCallback: widget.renderTextMaxLineCallback);
  }

  Widget renderBottomBtns() {
    if (widget.bottomBtnsView != null) {
      return widget.bottomBtnsView ?? Container();
    } else if (widget.bottomBtns != null) {
      List<Widget> btns = [];
      for (var i = 0; i < widget.bottomBtns!.length; i++) {
        btns.add(widget.bottomBtns![i]);
        if (widget.bottomBtns!.length - 1 != i) {
          btns.add(SizedBox(
            width: 8.r,
          ));
        }
      }
      return Container(
        // height: 48.r,
        padding: EdgeInsets.symmetric(horizontal: 16.r, vertical: 8.r),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.end,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: btns,
        ),
      );
    }
    return Container();
  }
}
